<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: AdminManageController.php 2010-08-31 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Page_AdminManageController extends Core_Controller_Action_Admin
{
	public function init()
  {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('page_admin_main', array(), 'page_admin_main_manage');
  }
  
	public function indexAction()
  {
    $table = Engine_Api::_()->getDbTable('pages', 'page');
  	$select = $table->select();

    $prefix = $table->getTablePrefix();

  	$select
  		->setIntegrityCheck(false)
  		->from($prefix.'page_pages')
  		->joinLeft($prefix.'page_fields_values', $prefix."page_fields_values.item_id = ".$prefix."page_pages.page_id")
  		->joinLeft($prefix.'page_fields_options', $prefix."page_fields_options.option_id = ".$prefix."page_fields_values.value AND ".$prefix."page_fields_options.field_id = 1", array("category" => $prefix."page_fields_options.label"));
  	
  	$this->view->filterForm = $filterForm = new Page_Form_Admin_Manage_Filter();
  	$page = $this->_getParam('page',1);
  	
    $values = array();
    if( $filterForm->isValid($this->_getAllParams()) ) {
      $values = $filterForm->getValues();
    }
    
  	foreach( $values as $key => $value ) {
      if( null === $value ) {
        unset($values[$key]);
      }
    }
    
    $values = array_merge(array(
      'order' => $prefix.'page_pages.page_id',
      'order_direction' => 'DESC',
    ), $values);
    
    $this->view->assign($values);
    
    $select->order(( !empty($values['order']) ? $values['order'] : $prefix.'page_pages.page_id' ) . ' ' . ( !empty($values['order_direction']) ? $values['order_direction'] : 'DESC' ));
    
  	if( !empty($values['title']) ){
      $select->where($prefix.'page_pages.title LIKE ?', '%' . $values['title'] . '%');
    }
    
  	if( !empty($values['category']) && $values['category'] != -1){
      $select
      	->where($prefix.'page_fields_values.field_id = 1 AND '.$prefix.'page_fields_values.value = ?', $values['category'] );
    }
    elseif (isset($values['category']) && $values['category'] == -1){
    	$select
      	->where($prefix.'page_fields_options.label IS NULL');
    }
    
  	if( isset($values['approved']) && $values['approved'] != -1 ){
      $select->where($prefix.'page_pages.approved = ?', $values['approved'] );
    }
    
  	if( isset($values['featured']) && $values['featured'] != -1 ){
      $select->where($prefix.'page_pages.featured = ?', $values['featured'] );
    }
  	
  	$select->where($prefix."page_pages.name <> 'footer' AND ".$prefix."page_pages.name <> 'header'");
  	$select->group($prefix."page_pages.page_id");

    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage(40);
    $this->view->paginator = $paginator->setCurrentPageNumber($page);	
  }
  
	public function approveAction()
  {
  	$id = $this->_getParam('id');
  	$value = $this->_getParam('value');
  	
  	if ($id){
  		$page = Engine_Api::_()->getItem('page', $id);
  	}
  	
  	$page->approvedStatus($value);
  	return $this->_redirectCustom($this->view->url(array('action' => 'index'), 'page_admin_manage', true));
  }
  
	public function featureAction()
  {
  	$id = $this->_getParam('id');
  	$value = $this->_getParam('value');
  	
  	if ($id){
  		$page = Engine_Api::_()->getItem('page', $id);
  	}
  	
  	$page->featuredStatus($value);
  	return $this->_redirectCustom($this->view->url(array('action' => 'index'), 'page_admin_manage', true));
  }
  
  public function deleteAction()
  {
  	$id = $this->_getParam('id');
  	
  	if ($id){
  		$page = Engine_Api::_()->getItem('page', $id);
  	}
  	
  	$pageTable = Engine_Api::_()->getDbTable('pages', 'page');
  	
  	$pageTable->deletePage($page);
  	return $this->_redirectCustom($this->view->url(array('action' => 'index'), 'page_admin_manage', true));
  }
  
	public function deleteAllAction()
  {
  	$ids = $this->_getParam('delete');
  	
  	if (!empty($ids)){
  		$pageTable = Engine_Api::_()->getDbTable('pages', 'page');
  		$pageTable->deletePages($ids);
  	}
  	
  	return $this->_redirectCustom($this->view->url(array('action' => 'index'), 'page_admin_manage', true));
  }
  
}